﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace GameSnake
{
    public class Snake
    {  
        public Snake()
        {
            this._SnakeBody = new List<SnakeItem>();

        }
       public Snake(CentrePoint cp, int lengthOfSnake)
            :this()
        {            
            this.head = new SnakeItem(cp._L,cp._T);
            SnakeBody.Add(head);

            for (int i = 1; i < lengthOfSnake; i++)
            {
                SnakeItem neck = new SnakeItem(head._X + i, head._Y);
                SnakeBody.Add(neck);
            }
        }


       public SnakeItem head;

        public int LengthOfSnake = 4;  //Defolt lengs of my snake.
        public class SnakeItem
        {
            public int _X { get; set; }
            public int _Y { get; set; }

            
            public SnakeItem()
            {
                this._X = 0;
                this._Y = 0;
            }
            public SnakeItem(int x, int y)
                :this()
            {
                _X = x;
                _Y = y;
            }
        }
        public SnakeDirection direct { get; set; }

        private List<SnakeItem> _SnakeBody;
        public List<SnakeItem> SnakeBody
        {
            get {return _SnakeBody;}
            set { _SnakeBody = value; }
        }      
        public void Move(Snake snake)
        {
              if (Console.KeyAvailable == true)
            {               
                var key = Console.ReadKey().Key;
                
                switch (key)
                {
                    case ConsoleKey.RightArrow:
                        snake._SnakeBody[0]._X += 1;
                        for (int i = 1; i < snake._SnakeBody.Count; i++)
                            snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        this.direct = SnakeDirection.Right;
                        break;
                    case ConsoleKey.UpArrow:
                        snake._SnakeBody[0]._Y -= 1;
                        for (int i = 1; i < snake._SnakeBody.Count; i++)
                                snake._SnakeBody[i] = snake._SnakeBody[i - 1];                            
                        this.direct = SnakeDirection.Up;
                        break;

                    case ConsoleKey.DownArrow:
                        snake._SnakeBody[0]._Y += 1;
                        for (int i = 1; i < snake._SnakeBody.Count; i++)
                            snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        this.direct = SnakeDirection.Down;
                        break;

                    case ConsoleKey.LeftArrow:
                        snake._SnakeBody[0]._X -= 1;
                        for (int i = 1; i < snake._SnakeBody.Count; i++)
                            snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        this.direct = SnakeDirection.left;
                        break;
                }
            }

            else
            {
                switch (direct)
                {
                    case SnakeDirection.Right:
                         snake._SnakeBody[0]._X += 1;
                         for (int i = 1; i < snake._SnakeBody.Count; i++)
                             snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        break;

                    case SnakeDirection.Up:
                        snake._SnakeBody[0]._Y -= 1;
                        for (int i = 1; i < snake._SnakeBody.Count; i++)
                            snake._SnakeBody[i] = snake._SnakeBody[i-1];
                        
                        break;

                    case SnakeDirection.Down:
                         snake._SnakeBody[0]._Y += 1;
                         for (int i = 1; i < snake._SnakeBody.Count; i++)
                             snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        break;

                    case SnakeDirection.left:
                         snake._SnakeBody[0]._X -= 1;
                         for (int i = 1; i < snake._SnakeBody.Count; i++)
                             snake._SnakeBody[i] = snake._SnakeBody[i - 1];
                        break;
                }                
            }
        }
         public enum SnakeDirection
        {
            Up,
            Down,
            Right,
            left,
            pause
        }

        }    

    }

    

